<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addrole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" width="50" :index="index" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >

              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title=" isEdit ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRoles, addRole, updateRole } from '@/api/setting'

export default {
  name: 'Settings',
  data() {
    return {

      isEdit: false,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },

      rules: {
        name: [],
        description: []
      },

      activeName: 'first',
      // 省略其他 ...
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },

      total: 0, // 总的数据条数
      roles: []
    }
  },
  computed: {
    // this.pageParams.page-1*this.pageParams.pagesize+1
    index() {
      return (this.pageParams.page - 1) * this.pageParams.pagesize + 1
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    addrole() {
      this.isEdit = false
      this.showDialog = true
    },
    async doRole() {
      await addRole(this.roleForm)
      this.loadRoles()
      this.showDialog = false
      this.$message.success('新增成功')
      // 跳转到最后页
      this.pageParams.page = Math.ceil(++this.total / this.pageParams.pagesize)
    },
    async updateRole() {
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.showDialog = false
        this.$message.success('编辑成功')
      } catch (err) {
        this.$message.error('编辑失败')
      }
    },
    hEdit(row) {
      this.isEdit = true
      this.showDialog = true
      this.roleForm = { ...row }
    },
    hDel(id) {
      this.$confirm('确定要删除吗？', '友情提示').then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      // 1.删除
      const res = await delRoles(id)
      console.log(res)
      // 2.提示
      this.$message.success('删除成功')
      if (this.roles.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page < 1) {
          this.pageParams.page = 1
        }
      }
      // 3. 重发请求
      this.loadRoles()
    },
    hSubmit() {
      if (this.isEdit) {
        this.updateRole()
      } else {
        this.doRole()
      }
    },
    hCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.pageParams.page = curPage
      // 2. 重发请求
      this.loadRoles()
    },
    handleSizeChange(curPageSize) {
      this.pageParams.pagesize = curPageSize
      // 2. 重发请求
      this.loadRoles()
    },
    hClose() {
      // 重置表单
      this.roleForm = { // 表单数据项
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }

  }
}
</script>
